﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace VRacer
{
    /// <summary>
    /// Przechowuje statystyki czasowych reakcji gracza. Czyli ile czasu mu zajmuje pomyslenie nad nastepnym ruchem itp itd.
    /// </summary>
    public class TimeStatistics : INotifyPropertyChanged
    {
        private int _averageTimeForTurn = 0;
        private long _lastTurnTime = 0;
        private long _totalTurnTime = 0;
        private int _turns = 0;
        private long _start;

        public int AverageTimeForTurn
        {
            get { return _averageTimeForTurn; }
            set
            {
                _averageTimeForTurn = value;
                OnPropertyChanged("AverageTimeForTurn");
            }
        }

        public long LastTurnTime
        {
            get { return _lastTurnTime; }
            set
            {
                _lastTurnTime = value;
                OnPropertyChanged("LastTurnTime");
            }
        }

        public long TotalTurnTime
        {
            get { return _totalTurnTime; }
            set
            {
                _totalTurnTime = value;
                OnPropertyChanged("TotalTurnTime");
            }
        }

        public void Start()
        {
            _turns++;
            _start = DateTime.Now.Ticks;
        }

        public void End()
        {
            var end = DateTime.Now.Ticks;
            LastTurnTime = (end - _start) / 10000000;
            TotalTurnTime += LastTurnTime;
            AverageTimeForTurn = (int)(TotalTurnTime / _turns);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
